package com.yonyou.pmclouds.team.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.exception.IllegalRequestArgException;
import com.yonyou.pmclouds.basecom.util.ChineseStringUtils;
import com.yonyou.pmclouds.outter.enterprise.EnterpriseInfoQuery;
import com.yonyou.pmclouds.team.entity.TeamEnterpriseVO;
import com.yonyou.pmclouds.team.rmiitf.TeamEnterpriseMaintain;
import com.yonyou.pmclouds.team.rmiitf.TeamEnterpriseQuery;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Comparator;

@RestController
@RequestMapping("/teamEnt")
public class TeamEnterpriseController {
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private TeamEnterpriseQuery query;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private TeamEnterpriseMaintain maintain;
    @Autowired
    private EnterpriseInfoQuery enterpriseInfoQuery;

    @GetMapping("/count")
    public int countEnterpriseOfProject(@RequestParam("projectid") String projectid) throws BusinessException {
        return query.countProjectEnterprise(projectid);
    }

    @GetMapping("/ents")
    public TeamEnterpriseVO[] getEnterpriseOfProject(@RequestParam("projectid") String projectid) throws BusinessException {
        TeamEnterpriseVO[] result = query.queryByProject(projectid);
        enterpriseInfoQuery.tranEntInfoField(result, new String[]{TeamEnterpriseVO.PK_ENTERPRISE}, new String[]{TeamEnterpriseVO.ENTERPRISE_NAME});
        if (ArrayUtils.isEmpty(result)) {
            return result;
        }
        Arrays.sort(result, new Comparator<TeamEnterpriseVO>() {
            @Override
            public int compare(TeamEnterpriseVO o1, TeamEnterpriseVO o2) {
                if (o1 == o2) {
                    return 0;
                }
                if (o1 == null) {
                    return 1;
                }
                if (o2 == null) {
                    return -1;
                }
                if (!o1.getEntRole().equals(o2.getEntRole())) {
                    return o1.getEntRole().compareTo(o2.getEntRole());
                }
                return ChineseStringUtils.compareInDict(o1.getEnterpriseName(), o2.getEnterpriseName());
            }
        });
        return result;
    }

    @PostMapping("/add")
    public TeamEnterpriseVO[] addTeamEnterprise(@RequestBody TeamEnterpriseVO[] enterprises, BindingResult result) throws BusinessException {
        if (result.hasErrors()) {
            throw new IllegalRequestArgException();
        }
        String[] added = maintain.addEnterpriseToProject(enterprises[0].getPkProject(), enterprises);
        return query.queryByIds(added);
    }

    @PostMapping("/remove")
    public String removeTeamEnterprise(@RequestBody @Validated TeamEnterpriseVO enterprise, BindingResult result) throws BusinessException {
        if (result.hasErrors()) {
            throw new IllegalRequestArgException();
        }
        return maintain.removeEnterpriseOfTeam(enterprise);
    }
}
